<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2021/3/9
 * Time: 16:09
 */

namespace app\api\controller;


use think\Db;

class Task extends Base
{
    public $user_id;
    public $create_time;
    public $page;
    
    /**
     * 异地登录验证
     * @return string
     */
    public function __construct()
    {
        $this->user_id = input('user_id');
        $this->page = input('page') ? input('page') : 1;
        $this->create_time = date('Y-m-d H:i:s');
        $time = input('time');
        $value = input('value');
        $reqKey = input('reqKey');
        $auth = $this->checkAll($this->user_id, $time, $value, $reqKey);
        if($auth == 112){
            echo json_encode(['status' => 301, 'msg' => $this->otherLogin]);
            exit;
        }elseif($auth == 110){
            echo json_encode(['status' => 302, 'msg' => $this->frost]);
            exit;
        }elseif($auth == 2111){
            echo json_encode(['status' => 303, 'msg' => $this->del]);
            exit;
        }
    }
    
    /**
     * 任务列表
     * @return \think\response\Json
     * page    页码
     */
    public function taskList(){
        $task = Db::table('up_task_user')
            ->join('up_task', 'task_id = tu_task_id' , 'LEFT')
            ->where('tu_user_id', $this->user_id)
            ->where('task_status', 1)
            ->field('tu_id, tu_task_id, task_title, tu_admin_is_check, tu_user_is_commit, task_time')
            ->order('task_time desc')
            ->page($this->page, 10)
            ->select();
        if($task){
            return json(['status' => 200, 'msg' => $task]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 任务详情
     * @return \think\response\Json
     * task_id     任务ID
     */
    public function taskDetail(){
        $task_id = input('task_id');
        $detail = Db::table('up_task')
            ->where('task_id', $task_id)
            ->field('task_id, task_title, task_type, task_content, task_images, task_video, task_link, 
            task_time')
            ->find();
        if($detail['task_images']){
            $detail['task_images'] = explode(',', $detail['task_images']);
        }else{
            $detail['task_images'] = [];
        }
        return json(['status' => 200, 'msg' => $detail]);
    }
    
    /**
     * 店员列表
     * @return \think\response\Json
     */
    public function shopAssistant(){
        $user = $this->userInfo();
        if($user['user_is_shopkeeper'] != 1) return json(['status' => 0, 'msg' => '您不是店长']);
        $shopAssistant = Db::table('up_u_user')
            ->where('user_shop_id', $user['user_shop_id'])
            ->where('user_is_shopkeeper', 2)
            ->where('user_status', 1)
            ->field('user_id, user_phone, user_username')
            ->select();
        if($shopAssistant){
            return json(['status' => 200, 'msg' => $shopAssistant]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 删除店员
     * @return \think\response\Json
     * other_user_id   店员ID
     */
    public function delShopAssistant(){
        $other_user_id = input('other_user_id');
        $user = $this->userInfo();
        if($user['user_is_shopkeeper'] != 1) return json(['status' => 0, 'msg' => '您不是店长']);
        $res = Db::table('up_u_user')
            ->where('user_id', $other_user_id)
            ->update(['user_is_shopkeeper' => 3,'user_shop_id' => null, 'user_district_id' => null]);
        if($res){
            return json(['status' => 200, 'msg' => '操作成功']);
        }else{
            return json(['status' => 0, 'msg' => '操作失败']);
        }
    }
    
    /**
     * 搜索用户
     * @return \think\response\Json
     * keyword   关键字/手机号
     */
    public function searchShopAssistant(){
        $keyword = input('keyword');
        if(isset($keyword) && $keyword != ''){
            $where = [
                'user_phone' => ['like', "%$keyword%"],
                'user_is_shopkeeper' => 3,
                'user_status' => 1,
                'user_from' => 1
            ];
            $user = Db::table('up_u_user')->where($where)->field('user_id, user_phone, user_username')->select();
            if($user){
                return json(['status' => 200, 'msg' => $user]);
            }else{
                return json(['status' => 200, 'msg' => []]);
            }
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 添加店员
     * @return \think\response\Json
     * other_user_id    店员ID
     */
    public function addShopAssistant(){
        $other_user_id = input('other_user_id');
        $user = $this->userInfo();
        $res = Db::table('up_u_user')
            ->where('user_id', $other_user_id)
            ->update(['user_is_shopkeeper' => 2, 'user_shop_id' => $user['user_shop_id']]);
        if($res){
            return json(['status' => 200, 'msg' => '添加成功']);
        }else{
            return json(['status' => 0, 'msg' => '添加失败']);
        }
    }
    
    /**
     * 转发任务给店员
     * @return \think\response\Json
     * task_id     任务ID
     * shopAssistant_ids    店员ID   逗号隔开
     */
    public function toShopAssistant(){
        $task_id = input('task_id');
        $shopAssistant_ids = input('shopAssistant_ids');
        $shopAssistant_arr = explode(',', trim($shopAssistant_ids, ','));
        // 去掉已经转发过的店员
        $already_in = Db::table('up_task_user')
            ->where('tu_task_id', $task_id)
            ->where('tu_user_type', 2)
            ->column('tu_user_id');
        $same = array_intersect($shopAssistant_arr, $already_in);
        $new = array_diff($shopAssistant_arr, $same);
        
        $count = count($new);
        if($count > 0){
            $insert_all = [];
            foreach($new as $k => $v){
                $insert_all[$k] = [
                    'tu_user_id' => $v,
                    'tu_user_type' => 2,
                    'tu_task_id' => $task_id,
                    'tu_time' => $this->create_time
                ];
            }
            $res = Db::table('up_task_user')->insertAll($insert_all);
            if($res){
                // todo:给店员推送消息
                return json(['status' => 200, 'msg' => '转发成功']);
            }else{
                return json(['status' => 0, 'msg' => '转发失败']);
            }
        }else{
            return json(['status' => 200, 'msg' => true]);
        }
    }
    
    /**
     * 增加任务转发数量
     * @return \think\response\Json
     * task_id    任务ID
     */
    public function shareTask(){
        $task_id = input('task_id');
        $res = Db::table('up_task')->where('task_id', $task_id)->setInc('task_share_number');
        if($res){
            return json(['status' => 200, 'msg' => true]);
        }else{
            return json(['status' => 0, 'msg' => false]);
        }
    }
    
    /**
     * 该任务的完成记录
     * @return \think\response\Json
     * task_id     任务ID
     */
    public function finishTaskList(){
        $task_id = input('task_id');
        $list = Db::table('up_task_finish')
            ->where(['bf_user_id' => $this->user_id, 'bf_task_id' => $task_id])
            ->field('bf_id, bf_image, bf_time')
            ->order('bf_time desc')
            ->select();
        if($list){
            foreach($list as $k => $v){
                $list[$k]['bf_image'] = explode(',', $v['bf_image']);
            }
            return json(['status' => 200, 'msg' => $list]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 上传任务完成图片
     * @return \think\response\Json
     * task_id      任务ID
     * tu_id        用户任务记录ID
     * task_images   图片,逗号隔开
     */
    public function uploadImages(){
        $task_id = input('task_id');
        $tu_id = input('tu_id');
        $task_images = trim(input('task_images'), ',');
        $insert = [
            'bf_user_id' => $this->user_id,
            'bf_task_id' => $task_id,
            'bf_tu_id' => $tu_id,
            'bf_image' => $task_images,
            'bf_time' => $this->create_time
        ];
        $res = Db::table('up_task_finish')->insert($insert);
        if($res){
            return json(['status' => 200, 'msg' => '上传成功']);
        }else{
            return json(['status' => 0, 'msg' => '上传失败']);
        }
    }
    
    /**
     * 提交完成任务
     * @return \think\response\Json
     * tu_id      用户任务记录ID
     */
    public function commitTask(){
        $tu_id = input('tu_id');
        $task_id = Db::table('up_task_user')->where('tu_id', $tu_id)->value('tu_task_id');
        $exsit = Db::table('up_task_finish')
            ->where(['bf_user_id' => $this->user_id, 'bf_task_id' => $task_id])
            ->find();
        if(!$exsit) return json(['status' => 0, 'msg' => '您还没有上传过任务图片']);
        
        $res = Db::table('up_task_user')->where('tu_id', $tu_id)
            ->update(['tu_user_is_commit' => 2, 'tu_user_commit_time' => $this->create_time]);
        if($res){
            return json(['status' => 200, 'msg' => '提交成功']);
        }else{
            return json(['status' => 0, 'msg' => '提交失败']);
        }
    }
    
    /**
     * 用户基本信息
     * @return array|false|\PDOStatement|string|\think\Model
     */
    private function userInfo(){
        $user = Db::table('up_u_user')
            ->where('user_id', $this->user_id)
            ->field('user_is_shopkeeper, user_shop_id')
            ->find();
        return $user;
    }
}